package com.example.shortmessage.ui.activity;

import android.text.InputFilter;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;

import androidx.annotation.NonNull;

import com.example.shortmessage.base.BaseActivity;
import com.example.shortmessage.base.BaseStyle;
import com.example.shortmessage.contract.AccountContract;
import com.example.shortmessage.data.constant.AccountConst;
import com.example.shortmessage.data.constant.FontsConst;
import com.example.shortmessage.data.entity.Account;
import com.example.shortmessage.databinding.ActivityAccountBinding;
import com.example.shortmessage.presenter.AccountPresenter;
import com.example.shortmessage.tips.dialog.WaitingDialog;

public class AccountActivity extends BaseActivity<ActivityAccountBinding, AccountContract.View<AccountContract.Presenter>,AccountContract.Presenter>
        implements AccountContract.View<AccountContract.Presenter> {

    private String hint;

    @NonNull
    @Override
    protected AccountContract.Presenter initPresenter() {
        Account account = (Account) getIntent().getSerializableExtra(AccountConst.SIGN);
        return new AccountPresenter(account);
    }

    @Override
    protected ActivityAccountBinding getLayoutView() {
        return ActivityAccountBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        setStyle();
        mBinding.title.tvTitle.setText(mPresenter.getTitle());
        mBinding.etContent.setHint(hint);
        mBinding.btnLogin.setText(mPresenter.getButton());
        mBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String submit = mPresenter.submit(mBinding.etContent.getText().toString(), AccountActivity.this);
                if(submit!=null){
                    mBinding.etContent.setError(submit);
                }
            }
        });
    }

    @Override
    protected void setStyle() {
        setStyleSize();
        hint = mPresenter.getHint();
        mBinding.title.tvTitle.setVisibility(View.VISIBLE);
        if(hint.equals(AccountConst.HINT_RE_USER)||hint.equals(AccountConst.HINT_PASSWORD_USER)){
            mBinding.etContent.setInputType(InputType.TYPE_CLASS_TEXT);
            mBinding.etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
        }else {
            mBinding.etContent.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
            mBinding.etContent.setTransformationMethod(PasswordTransformationMethod.getInstance());
            mBinding.etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});
        }
    }

    private void setStyleSize() {
        BaseStyle baseStyle = new BaseStyle();
        baseStyle.set(this,mBinding.title.tvTitle,FontsConst.TV_TITLE);
        baseStyle.set(this,mBinding.etContent,FontsConst.TV_CONTENT);
        baseStyle.set(this,mBinding.etContent,FontsConst.TV_CONTENT);
    }

    /***************************************************************/
    @Override
    public void setPresenter(@NonNull AccountContract.Presenter presenter) {
        mPresenter = presenter;

    }

    @Override
    public void showLoading() {
        WaitingDialog.getInstance().show(this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

}
